//
//  WorkSecondVC.m
//  XDCommonApp
//
//  Created by xindaoapp on 14-3-11.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "WorkSecondVC.h"
#import "XDTools.h"
#import "XDHeader.h"
#import "WaterDetailViewController.h"

#define dHeightForHeader 0

@interface WorkSecondVC ()

{
    NSArray *allArray;
}

@end

@implementation WorkSecondVC
@synthesize catID;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
     allArray = [[NSArray alloc]init];
    [self getXmlData];
}

- (void)initWidget
{
    self.titleLabel.text = _catname;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.titleLabel.textColor = TEXTBLACKCOLORE;
    
    self.detailActTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,.0f, UI_SCREEN_WIDTH,self.contentView.frame.size.height) style:UITableViewStylePlain];
    _detailActTableView.delegate = self;
    _detailActTableView.dataSource = self;
    _detailActTableView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_detailActTableView];
}


#pragma mark  --------------tableView delegate----------------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allArray.count+1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return dHeightForHeader;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //    NSArray * array;
    CGFloat headerHeight=0;
    if (tableView == _detailActTableView){
        
        //        array=@[@"2014年2月22日 星期一",@"2012年2月24日 星期三"];
        headerHeight = dHeightForHeader;
    }
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, headerHeight)];
    view.backgroundColor = RGBA(217, 217, 217, 1);
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, UI_SCREEN_WIDTH-15, headerHeight)];
    label.textColor = TEXTBLACKCOLORE;
    label.font = [UIFont systemFontOfSize:15];
    label.backgroundColor = [UIColor clearColor];
    //    label.text = [array objectAtIndex:section];
    //    label.text = activityDate;
    
    [view addSubview:label];
    return view;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    return 150;
    return 60;
}


-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == _detailActTableView){
        static NSString * cellName = @"cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
            cell.backgroundColor = [UIColor clearColor];
        }
        if (indexPath.row==0) {
            cell.textLabel.text = @"全部";
        }else{
        
            if (allArray.count >0) {
                cell.textLabel.text = [[allArray objectAtIndex:indexPath.row-1]valueForKey:@"categoryName"];
            }
            
        }
        UIImageView *nextLvIV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"launh_sure@2x"]];
        nextLvIV.frame = CGRectMake(290, 20, 15, 20);
        nextLvIV.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:nextLvIV];
        
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WaterDetailViewController *waterVc = [[WaterDetailViewController alloc] init];
    waterVc.classID = catID;
    waterVc.signal = @"0";
    
    if (indexPath.row==0) {
        
        waterVc.subID = @"";
    
        
    }else{

        waterVc.subID = [[allArray objectAtIndex:indexPath.row-1]valueForKey:@"categoryId"];
        
    }
    
    [self.navigationController pushViewController:waterVc animated:YES];
    DDLOG(@"跳转到瀑布流");
    
}

#pragma mark  -------- DataSouceManager------------

- (void)getXmlData
{
    if ([XDTools NetworkReachable]){
        NSString *body = [NSString stringWithFormat:@" <WorkListCategoryById xmlns=\"http://tempuri.org/\">"
                          "<id>%@</id>"
                          "</WorkListCategoryById>",catID];
        
        __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:body API:WORKLISTCATEGORYBYID];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            //[XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            // NSDictionary *tempDic = [XDTools  JSonFromString:responseString];
            DDLOG(@"responseString:%@", responseString);
            
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            int statusCode = [request responseStatusCode];
            NSString *soapAction = [[request requestHeaders] objectForKey:@"SOAPAction"];
            
            NSArray *arraySOAP =[soapAction componentsSeparatedByString:@"/"];
            int count = [arraySOAP count] - 1;
            NSString *methodName = [arraySOAP objectAtIndex:count];
            
            // Use when fetching text data
            NSString *result = nil;
            if (statusCode == 200) {
                //表示正常请求
                result = [SoapXmlParseHelper SoapMessageResultXml:responseString ServiceMethodName:methodName];
                NSDictionary  *responseDict = [XDTools JSonFromString:result];
                if ([[responseDict objectForKey:@"code"] intValue]==0){
                    DDLOG(@"res = %@",responseDict);
                    
                    allArray = [responseDict valueForKey:@"categoryList"];
          
                    [self initWidget];
                    
                    
                }
            }
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
        }];
        
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
